<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 新增 -->
      <div style="display: inline-block;margin: 0px 2px;">
        <el-button
          v-permission="['ADMIN','ACT_PROCESS_ALL','ACT_PROCESS_DEPLOY']"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-upload"
          @click="add">流程导入</el-button>
      </div>
    </div>
    <!--表单组件-->
    <eForm ref="form"/>
    <!--表格渲染-->
    <el-table v-loading="loading" ref="table"  :data="data" size="small" style="width: 100%;" border>
      <el-table-column type="selection" width="55"/>
      <el-table-column prop="category" label="流程分类"/>
      <el-table-column prop="key" label="流程标识"/>
      <el-table-column prop="name" label="流程名称"/>
      <el-table-column prop="version" label="流程版本"/>
      <el-table-column :show-overflow-tooltip="true" prop="resourceName" label="流程XML">
        <template slot-scope="scope">
          <el-link :underline="false" target="_blank" type="primary" @click="flowInfoAction(scope.row.id,'xml')" >{{ scope.row.resourceName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="diagramResourceName" label="流程图片">
        <template slot-scope="scope">
          <el-link :underline="false" target="_blank" type="primary" @click="flowInfoImge(scope.row.id,'image')" >{{ scope.row.diagramResourceName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="部署时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deploymentTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button v-permission="['ADMIN','ACT_PROCESS_ALL','ACT_PROCESS_UPDATE']" v-if="scope.row.suspended" size="mini" type="text" @click="hangUp(scope.row.id,'active')">[激活]</el-button>
          <el-button v-permission="['ADMIN','ACT_PROCESS_ALL','ACT_PROCESS_UPDATE']" v-else size="mini" type="text" @click="hangUp(scope.row.id,'suspend')">[挂起]</el-button>
          <el-button v-permission="['ADMIN','ACT_PROCESS_ALL','ACT_PROCESS_DELETE']" size="mini" type="text" @click="deleteFlow(scope.row.deploymentId)">[删除]</el-button>
          <el-button v-permission="['ADMIN','ACT_PROCESS_ALL','ACT_PROCESS_TRANSFORM']" size="mini" type="text" @click="transformModel(scope.row.id)">[转换为模型]</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialog" title="流程详情" append-to-body top="10px" width="85%" style="overflow: auto;">
      <span v-if="xmlTrue">{{ flowInfo }}</span>
      <img v-else :src="flowInfo" width="100%">
    </el-dialog>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { parseTime } from '@/utils/index'
import { mapGetters } from 'vuex'
import eForm from './form'
import { del, flowHangUp, transformModel, resourceReadImg, resourceReadXml } from '@/api/framework/flow/flowProcess'

export default {
  components: { eForm },
  mixins: [initData],
  data() {
    return {
      loading: false,
      dialog: false,
      delAllLoading: false,
      flowInfo: '',
      xmlTrue: true
    }
  },
  computed: {
    ...mapGetters([
      'baseApi'
    ])
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    checkPermission,
    parseTime,
    beforeInit() {
      this.url = 'flowProcess/list'
      const sort = 'id,desc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      const type = query.type
      const value = query.value
      if (type && value) { this.params[type] = value }
      return true
    },
    add() {
      this.$refs.form.dialog = true
    },
    hangUp(procDefId, state) {
      this.loading = true
      flowHangUp(procDefId, state).then(res => {
        this.loading = false
        this.$notify({
          title: '操作成功',
          type: 'success',
          duration: 2500
        })
        this.init()
      }).catch(err => {
        this.loading = false
      })
    },
    deleteFlow(deploymentId) {
      this.loading = true
      del(deploymentId).then(res => {
        this.loading = false
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
        this.init()
      }).catch(err => {
        this.loading = false
      })
    },
    transformModel(procDefId) {
      this.loading = true
      transformModel(procDefId).then(res => {
        this.loading = false
        this.$notify({
          title: '转换为模型功',
          type: 'success',
          duration: 2500
        })
        this.init()
      }).catch(err => {
        this.loading = false
      })
    },
    flowInfoAction(procDefId, resType) {
      this.dialog = true
      this.xmlTrue = true
      resourceReadXml(procDefId, resType).then(res => {
        this.flowInfo = res;
      }).catch(err => {
        this.dialog = false
      })
    },
    flowInfoImge(procDefId, resType) {
      this.dialog = true
      this.xmlTrue = false
      resourceReadImg(procDefId, resType).then(res => {
        this.flowInfo = 'data:image/png;base64,' + btoa(
          new Uint8Array(res)
            .reduce((data, byte) => data + String.fromCharCode(byte), '')
        )
      }).catch(err => {
        this.dialog = false
      })
    }
  }
}
</script>

<style>

</style>
